package com.mc.fil.service.impl;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilMultimedia;
import com.mc.fil.domain.FilWatering;
import com.mc.fil.mapper.FilMultimediaMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilWeedingMapper;
import com.mc.fil.domain.FilWeeding;
import com.mc.fil.service.IFilWeedingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 除草 Service业务层处理
 *
 * @author mc
 * @date 2021-04-14
 */
@Service
public class FilWeedingServiceImpl implements IFilWeedingService
{
    @Autowired
    private FilWeedingMapper filWeedingMapper;
    @Autowired
    private FilMultimediaMapper multimediaMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询除草
     *
     * @param id 除草 ID
     * @return 除草
     */
    @Override
    public FilWeeding selectFilWeedingById(Long id)
    {
        FilWeeding weeding = filWeedingMapper.selectFilWeedingById(id);
        FilMultimedia multimedia = new FilMultimedia();
        multimedia.setFilId(id);
        multimedia.setGenre("filWeeding");
        weeding.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        return weeding;
    }

    /**
     * 查询除草 列表
     *
     * @param filWeeding 除草
     * @return 除草
     */
    @Override
    public List<FilWeeding> selectFilWeedingList(FilWeeding filWeeding)
    {
        List<FilWeeding> list = filWeedingMapper.selectFilWeedingList(filWeeding);
        for(FilWeeding weeding : list){
            FilMultimedia multimedia = new FilMultimedia();
            multimedia.setFilId(weeding.getId());
            multimedia.setGenre("filWeeding");
            weeding.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        }
        return list;
    }

    /**
     * 新增除草
     *
     * @param filWeeding 除草
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFilWeeding(FilWeeding filWeeding)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filWeeding.setCreateTime(DateUtils.getNowDate());
        filWeeding.setCreateUserId(loginUser.getUsername());
        filWeedingMapper.insertFilWeeding(filWeeding);
        List<FilMultimedia> list = filWeeding.getPhotos();
        for(FilMultimedia fil : list){
            fil.setFilId(filWeeding.getId());
            fil.setCreatedBy(loginUser.getUsername());
            fil.setCreatedTime(DateUtils.getNowDate());
            fil.setGenre("filWeeding");
            multimediaMapper.insertFilMultimedia(fil);
        }
        return 1;
    }

}